<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>留言板</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    input {
      background: none;
      outline: none;
      border: 1px solid #ccc;
    }

    .box {
      width: 500px;
      background-color: orange;
      margin: 100px auto;
    }

    .box .top {
      height: 350px;
      position: relative;
      border-bottom: 10px solid yellowgreen;
    }

    .top p:nth-child(1) input[type="text"] {
      background-color: #fff;
      height: 30px;
      width: 200px;
      line-height: 30px;
      font-size: 18px;
      margin-top: 10px;
    }

    .top p:nth-child(2) textarea {
      margin: 10px 0;
      width: 300px;
      height: 200px;
      resize: none;
    }

    .top p:nth-child(3) {
      width: 80px;
      height: 80px;
      position: absolute;
      right: 0;
      font-size: 18px;
    }

    .top p:nth-child(3) input {
      width: 15px;
      height: 15px;
    }

    .top p:nth-child(4) {
      position: absolute;
      right: 30px;
      bottom: 20px;
    }

    .top p:nth-child(4) button {
      width: 80px;
      height: 30px;
      font-size: 18px;
    }

    .bottom .content {
      text-indent: 4em;
    }
  </style>
</head>

<body>
  <div class="box">
    <div class="top">
      <p>昵称：<input type="text" id="val" required></p>
      <p>
        <textarea name="" id="txt" required></textarea>
      </p>
      <p><input type="checkbox" id="check">匿名</p>
      <p><button id="btn">发表</button></p>
    </div>
    <div class="bottom"></div>
  </div>
  <script src="js/jquery.1.12.4.js"></script>
  <script>
    // 获取元素
    let $val = $('#val');
    let $txt = $('#txt');
    let $check = $('#check');
    let $btn = $('#btn');
    let $btm = $('.bottom');

    // 设置一些中间变量
    let name = null;
    // 点击按钮
    $btn.on('click', function () {
      // 判断昵称和内容是否为空
      if ($val.val() == '' || $txt.val() == '') {
        alert('昵称和内容不能为空');
      } else {
        // 判断匿名发布是否选中
        if ($check.prop('checked')) {
          name = '你猜';
        } else {
          name = $val.val();
        }
        let time = showtime();

        let $html = `
          <p>${name} ${time}</p>
          <p class="content">${$txt.val()}</p>
        `;

        $btm.append($html);
      }

    });

    function showtime() {
      var now = new Date();
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds()
      var timeValue = "" + ((hours >= 12) ? "下午 " : "上午 ")
      timeValue += ((hours > 12) ? hours - 12 : hours)
      timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
      timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
      return timeValue;
    }
    console.log(showtime());
  </script>
</body>

</html>